﻿<form id="form">
    <div>用户名：<input type="text" id=userName name="userName" value="admin" /></div>
    <div>密 &nbsp; &nbsp;码：<input type="password" id="password" name="password" value="123456" /></div>
    <div>
        <input type="button" value="登  录" onclick="login()" />
    </div>
</form>

<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/js/Common.js"></script>
<script>
    sessionCheck();
    //会话检查
    function sessionCheck() {
        //获取参数集合
        const urlParams = GetParam();
        const clientId = urlParams['clientId'];
        const redirectUrl = urlParams['redirectUrl'];
        const sessionCode = getCookie("SessionCode");
        if (!sessionCode) {
            return;
        }
        //根据授权码获取code
        var params = { clientId, sessionCode }
        $.ajax({
            url: '/SSO/GetCodeBySessionCode',
            data: JSON.stringify(params),
            method: 'post',
            dataType: 'json',
            contentType: 'application/json',
            success: function (data) {
                if (data.code === 0) {
                    const code = data.data;
                    window.location.href = redirectUrl + '?authCode=' + code + "&sessionCode=" + sessionCode;
                }
            }
        })
    }

    function login() {
        const urlParams = GetParam();
        const clientId = urlParams['clientId'];
        const redirectUrl = urlParams['redirectUrl']
        const userName = $("#userName").val()
        const password = $("#password").val()
        const params = { clientId, userName, password }
        $.ajax({
            url: '/SSO/GetCode',
            data: JSON.stringify(params),
            method: 'post',
            dataType: 'json',
            contentType: "application/json",
            success: function (data) {
                //获得code，跳转回客户页面
                if (data.code === 0) {
                    const code = data.data

                    //存储会话,这里的时间最好减去几分钟，不然那边的token过期，这里刚好多了几秒没过期又重新登录了
                    setCookie("SessionCode", code, 24 * 60 * 60, "/")
                    window.location.href = redirectUrl + '?authCode=' + code + '&sessionCode=' + code
                } else {
                    alert(data.message)
                }
            }
        })
    }
</script>